
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>admin &mdash; Flask_Dashed 0.1b documentation</title>
    
    <link rel="stylesheet" href="../_static/flasky.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1b',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="top" title="Flask_Dashed 0.1b documentation" href="../index.html" />
    <link rel="up" title="Module code" href="index.html" /> 
  </head>
  <body>
  
  


    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for admin</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>
<span class="kn">from</span> <span class="nn">werkzeug</span> <span class="kn">import</span> <span class="n">OrderedMultiDict</span>

<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Blueprint</span><span class="p">,</span> <span class="n">url_for</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">abort</span>
<span class="kn">from</span> <span class="nn">views</span> <span class="kn">import</span> <span class="n">ObjectListView</span><span class="p">,</span> <span class="n">ObjectFormView</span>
<span class="kn">from</span> <span class="nn">views</span> <span class="kn">import</span> <span class="n">ObjectDeleteView</span><span class="p">,</span> <span class="n">secure</span>


<div class="viewcode-block" id="recursive_getattr"><a class="viewcode-back" href="../api.html#admin.recursive_getattr">[docs]</a><span class="k">def</span> <span class="nf">recursive_getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns object related attributes, as it&#39;s a template filter None</span>
<span class="sd">    is return when attribute doesn&#39;t exists.</span>

<span class="sd">    eg::</span>

<span class="sd">        a = object()</span>
<span class="sd">        a.b = object()</span>
<span class="sd">        a.b.c = 1</span>
<span class="sd">        recursive_getattr(a, &#39;b.c&#39;) =&gt; 1</span>
<span class="sd">        recursive_getattr(a, &#39;b.d&#39;) =&gt; None</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">if</span> <span class="s">&quot;.&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">attr</span><span class="p">:</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">attr</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">l</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">recursive_getattr</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="s">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">None</span>

</div>
<div class="viewcode-block" id="AdminNode"><a class="viewcode-back" href="../api.html#admin.AdminNode">[docs]</a><span class="k">class</span> <span class="nc">AdminNode</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;An AdminNode just act as navigation container, it doesn&#39;t provide any</span>
<span class="sd">    rules.</span>

<span class="sd">    :param admin: The parent admin object</span>
<span class="sd">    :param url_prefix: The url prefix</span>
<span class="sd">    :param enpoint: The endpoint</span>
<span class="sd">    :param short_title: The short module title use on navigation</span>
<span class="sd">        &amp; breadcrumbs</span>
<span class="sd">    :param title: The long title</span>
<span class="sd">    :param parent: The parent node</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">admin</span><span class="p">,</span> <span class="n">url_prefix</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">short_title</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">admin</span> <span class="o">=</span> <span class="n">admin</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">parent</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">url_prefix</span> <span class="o">=</span> <span class="n">url_prefix</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">endpoint</span> <span class="o">=</span> <span class="n">endpoint</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">short_title</span> <span class="o">=</span> <span class="n">short_title</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">title</span> <span class="o">=</span> <span class="n">title</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">children</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="nd">@property</span>
<div class="viewcode-block" id="AdminNode.url_path"><a class="viewcode-back" href="../api.html#admin.AdminNode.url_path">[docs]</a>    <span class="k">def</span> <span class="nf">url_path</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns the url path relative to admin one.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">url_path</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">url_prefix</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">url_prefix</span>
</div>
    <span class="nd">@property</span>
<div class="viewcode-block" id="AdminNode.parents"><a class="viewcode-back" href="../api.html#admin.AdminNode.parents">[docs]</a>    <span class="k">def</span> <span class="nf">parents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns all parent hierarchy as list. Usefull for breadcrumbs.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="p">:</span>
            <span class="n">parents</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">parents</span><span class="p">)</span>
            <span class="n">parents</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">parents</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
</div>
<div class="viewcode-block" id="AdminNode.secure"><a class="viewcode-back" href="../api.html#admin.AdminNode.secure">[docs]</a>    <span class="k">def</span> <span class="nf">secure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">http_code</span><span class="o">=</span><span class="mi">403</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Gives a way to secure specific url path.</span>

<span class="sd">        :param http_code: The response http code when False</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">add_path_security</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url_path</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">http_code</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">f</span>
        <span class="k">return</span> <span class="n">decorator</span>

</div></div>
<div class="viewcode-block" id="Admin"><a class="viewcode-back" href="../api.html#admin.Admin">[docs]</a><span class="k">class</span> <span class="nc">Admin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Class that provides a way to add admin interface to Flask applications.</span>

<span class="sd">    :param app: The Flask application</span>
<span class="sd">    :param url_prefix: The url prefix</span>
<span class="sd">    :param main_dashboard: The main dashboard object</span>
<span class="sd">    :param endpoint: The endpoint</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app</span><span class="p">,</span> <span class="n">url_prefix</span><span class="o">=</span><span class="s">&quot;/admin&quot;</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s">&quot;flask-dashed&quot;</span><span class="p">,</span>
            <span class="n">main_dashboard</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">&#39;admin&#39;</span><span class="p">):</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">main_dashboard</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">dashboard</span> <span class="kn">import</span> <span class="n">DefaultDashboard</span>
            <span class="n">main_dashboard</span> <span class="o">=</span> <span class="n">DefaultDashboard</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">blueprint</span> <span class="o">=</span> <span class="n">Blueprint</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">__name__</span><span class="p">,</span>
            <span class="n">static_folder</span><span class="o">=</span><span class="s">&#39;static&#39;</span><span class="p">,</span> <span class="n">template_folder</span><span class="o">=</span><span class="s">&#39;templates&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app</span> <span class="o">=</span> <span class="n">app</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">url_prefix</span> <span class="o">=</span> <span class="n">url_prefix</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">endpoint</span> <span class="o">=</span> <span class="n">endpoint</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">title</span> <span class="o">=</span> <span class="n">title</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">secure_functions</span> <span class="o">=</span> <span class="n">OrderedMultiDict</span><span class="p">()</span>
        <span class="c"># Checks security for current path</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">blueprint</span><span class="o">.</span><span class="n">before_request</span><span class="p">(</span>
            <span class="k">lambda</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_path_security</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">app</span><span class="o">.</span><span class="n">register_blueprint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">blueprint</span><span class="p">,</span> <span class="n">url_prefix</span><span class="o">=</span><span class="n">url_prefix</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">root_nodes</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_add_node</span><span class="p">(</span><span class="n">main_dashboard</span><span class="p">,</span> <span class="s">&#39;/&#39;</span><span class="p">,</span> <span class="s">&#39;main-dashboard&#39;</span><span class="p">,</span> <span class="s">&#39;dashboard&#39;</span><span class="p">)</span>
        <span class="c"># Registers recursive_getattr filter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app</span><span class="o">.</span><span class="n">jinja_env</span><span class="o">.</span><span class="n">filters</span><span class="p">[</span><span class="s">&#39;recursive_getattr&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">recursive_getattr</span>

<div class="viewcode-block" id="Admin.register_node"><a class="viewcode-back" href="../api.html#admin.Admin.register_node">[docs]</a>    <span class="k">def</span> <span class="nf">register_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url_prefix</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">short_title</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">node_class</span><span class="o">=</span><span class="n">AdminNode</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Registers admin node.</span>

<span class="sd">        :param url_prefix: The url prefix</span>
<span class="sd">        :param endpoint: The endpoint</span>
<span class="sd">        :param short_title: The short title</span>
<span class="sd">        :param title: The long title</span>
<span class="sd">        :param parent: The parent node path</span>
<span class="sd">        :param node_class: The class for node objects</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_add_node</span><span class="p">(</span><span class="n">node_class</span><span class="p">,</span> <span class="n">url_prefix</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">short_title</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="n">title</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">parent</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="Admin.register_module"><a class="viewcode-back" href="../api.html#admin.Admin.register_module">[docs]</a>    <span class="k">def</span> <span class="nf">register_module</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">module_class</span><span class="p">,</span> <span class="n">url_prefix</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">short_title</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Registers new module to current admin.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_add_node</span><span class="p">(</span><span class="n">module_class</span><span class="p">,</span> <span class="n">url_prefix</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">short_title</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="n">title</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">parent</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">_add_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node_class</span><span class="p">,</span> <span class="n">url_prefix</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">short_title</span><span class="p">,</span>
        <span class="n">title</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Registers new node object to current admin object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">title</span> <span class="o">=</span> <span class="n">short_title</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">title</span> <span class="k">else</span> <span class="n">title</span>
        <span class="k">if</span> <span class="n">parent</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">parent</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">AdminNode</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;`parent` class must be AdminNode subclass&#39;</span><span class="p">)</span>
        <span class="n">new_node</span> <span class="o">=</span> <span class="n">node_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url_prefix</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">short_title</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="n">title</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">parent</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">parent</span><span class="p">:</span>
            <span class="n">parent</span><span class="o">.</span><span class="n">children</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_node</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">root_nodes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_node</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">new_node</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">main_dashboard</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">root_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

<div class="viewcode-block" id="Admin.add_path_security"><a class="viewcode-back" href="../api.html#admin.Admin.add_path_security">[docs]</a>    <span class="k">def</span> <span class="nf">add_path_security</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="n">http_code</span><span class="o">=</span><span class="mi">403</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Registers security function for given path.</span>

<span class="sd">        :param path: The endpoint to secure</span>
<span class="sd">        :param function: The security function</span>
<span class="sd">        :param http_code: The response http code</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">secure_functions</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">http_code</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="Admin.check_path_security"><a class="viewcode-back" href="../api.html#admin.Admin.check_path_security">[docs]</a>    <span class="k">def</span> <span class="nf">check_path_security</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Checks security for specific and path.</span>

<span class="sd">        :param path: The path to check</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">secure_functions</span><span class="o">.</span><span class="n">iterkeys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url_prefix</span><span class="p">,</span> <span class="n">key</span><span class="p">)):</span>
                <span class="k">for</span> <span class="n">function</span><span class="p">,</span> <span class="n">http_code</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">secure_functions</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="n">key</span><span class="p">):</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">function</span><span class="p">():</span>
                        <span class="k">return</span> <span class="n">abort</span><span class="p">(</span><span class="n">http_code</span><span class="p">)</span>

</div></div>
<div class="viewcode-block" id="AdminModule"><a class="viewcode-back" href="../api.html#admin.AdminModule">[docs]</a><span class="k">class</span> <span class="nc">AdminModule</span><span class="p">(</span><span class="n">AdminNode</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Class that provides a way to create simple admin module.</span>

<span class="sd">    :param admin: The parent admin object</span>
<span class="sd">    :param url_prefix: The url prefix</span>
<span class="sd">    :param enpoint: The endpoint</span>
<span class="sd">    :param short_title: the short module title use on navigation</span>
<span class="sd">        &amp; breadcrumbs</span>
<span class="sd">    :param title: The long title</span>
<span class="sd">    :param parent: The parent node</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AdminModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rules</span> <span class="o">=</span> <span class="n">OrderedMultiDict</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_register_rules</span><span class="p">()</span>

<div class="viewcode-block" id="AdminModule.add_url_rule"><a class="viewcode-back" href="../api.html#admin.AdminModule.add_url_rule">[docs]</a>    <span class="k">def</span> <span class="nf">add_url_rule</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">rule</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">view_func</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Adds a routing rule to the application from relative endpoint.</span>
<span class="sd">        `view_class` is copied as we need to dynamically apply decorators.</span>

<span class="sd">        :param rule: The rule</span>
<span class="sd">        :param endpoint: The endpoint</span>
<span class="sd">        :param view_func: The view</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">class</span> <span class="nc">ViewClass</span><span class="p">(</span><span class="n">view_func</span><span class="o">.</span><span class="n">view_class</span><span class="p">):</span>
            <span class="k">pass</span>

        <span class="n">ViewClass</span><span class="o">.</span><span class="n">__name__</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">)</span>
        <span class="n">ViewClass</span><span class="o">.</span><span class="n">__module__</span> <span class="o">=</span> <span class="n">view_func</span><span class="o">.</span><span class="n">__module__</span>
        <span class="n">view_func</span><span class="o">.</span><span class="n">view_class</span> <span class="o">=</span> <span class="n">ViewClass</span>
        <span class="n">full_endpoint</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">endpoint</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">app</span><span class="o">.</span><span class="n">add_url_rule</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s%s%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">url_prefix</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">url_path</span><span class="p">,</span> <span class="n">rule</span><span class="p">),</span> <span class="n">full_endpoint</span><span class="p">,</span> <span class="n">view_func</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rules</span><span class="o">.</span><span class="n">setlist</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="p">[(</span><span class="n">rule</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">view_func</span><span class="p">)])</span>
</div>
    <span class="k">def</span> <span class="nf">_register_rules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Registers all module rules after initialization.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&#39;default_rules&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s">&#39;Admin module class must provide&#39;</span>
                <span class="o">+</span> <span class="s">&#39; default_rules&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">rule</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">view_func</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">default_rules</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_url_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">view_func</span><span class="p">)</span>

    <span class="nd">@property</span>
<div class="viewcode-block" id="AdminModule.url"><a class="viewcode-back" href="../api.html#admin.AdminModule.url">[docs]</a>    <span class="k">def</span> <span class="nf">url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns first registered (main) rule as url.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">url_for</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">endpoint</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">endpoint</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rules</span><span class="o">.</span><span class="n">lists</span><span class="p">()[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]))</span>
                <span class="c"># Cause OrderedMultiDict.keys() doesn&#39;t preserve order...</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;`AdminModule` must provide at list one rule.&#39;</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="AdminModule.secure_endpoint"><a class="viewcode-back" href="../api.html#admin.AdminModule.secure_endpoint">[docs]</a>    <span class="k">def</span> <span class="nf">secure_endpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span>  <span class="n">http_code</span><span class="o">=</span><span class="mi">403</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Gives a way to secure specific url path.</span>

<span class="sd">        :param endpoint: The endpoint to protect</span>
<span class="sd">        :param http_code: The response http code when False</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_secure_enpoint</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">http_code</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">f</span>
        <span class="k">return</span> <span class="n">decorator</span>
</div>
    <span class="k">def</span> <span class="nf">_secure_enpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">secure_function</span><span class="p">,</span> <span class="n">http_code</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Secure enpoint view function via `secure` decorator.</span>

<span class="sd">        :param enpoint: The endpoint to secure</span>
<span class="sd">        :param secure_function: The function to check</span>
<span class="sd">        :param http_code: The response http code when False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">rule</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">view_func</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rules</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">endpoint</span><span class="p">)</span>
        <span class="n">view_func</span><span class="o">.</span><span class="n">view_class</span><span class="o">.</span><span class="n">dispatch_request</span> <span class="o">=</span>\
            <span class="n">secure</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">secure_function</span><span class="p">,</span> <span class="n">http_code</span><span class="p">)(</span>
                <span class="n">view_func</span><span class="o">.</span><span class="n">view_class</span><span class="o">.</span><span class="n">dispatch_request</span><span class="p">)</span>

</div>
<span class="k">class</span> <span class="nc">ObjectAdminModule</span><span class="p">(</span><span class="n">AdminModule</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class for object admin modules backends.</span>
<span class="sd">    Provides all required methods to retrieve, create, update and delete</span>
<span class="sd">    objects.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># List relateds</span>
    <span class="n">list_view</span> <span class="o">=</span> <span class="n">ObjectListView</span>
    <span class="n">list_template</span> <span class="o">=</span> <span class="s">&#39;flask_dashed/list.html&#39;</span>
    <span class="n">list_fields</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">list_title</span> <span class="o">=</span> <span class="s">&#39;list&#39;</span>
    <span class="n">list_per_page</span> <span class="o">=</span> <span class="mi">10</span>
    <span class="n">searchable_fields</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">order_by</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="c"># Edit relateds</span>
    <span class="n">edit_template</span> <span class="o">=</span> <span class="s">&#39;flask_dashed/edit.html&#39;</span>
    <span class="n">form_view</span> <span class="o">=</span> <span class="n">ObjectFormView</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">edit_title</span> <span class="o">=</span> <span class="s">&#39;edit object&#39;</span>
    <span class="c"># New relateds</span>
    <span class="n">new_title</span> <span class="o">=</span> <span class="s">&#39;new object&#39;</span>
    <span class="c"># Delete relateds</span>
    <span class="n">delete_view</span> <span class="o">=</span> <span class="n">ObjectDeleteView</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">cls</span><span class="o">.</span><span class="n">list_fields</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ObjectAdminModule</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">default_rules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Adds object list rule to current app.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span>
            <span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">,</span> <span class="s">&#39;list&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_view</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="s">&#39;short_title&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="p">)),</span>
            <span class="p">(</span><span class="s">&#39;/page/&lt;page&gt;&#39;</span><span class="p">,</span> <span class="s">&#39;list&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_view</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="s">&#39;short_title&#39;</span><span class="p">,</span>
                <span class="bp">self</span><span class="p">)),</span>
            <span class="p">(</span><span class="s">&#39;/new&#39;</span><span class="p">,</span> <span class="s">&#39;new&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_view</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="s">&#39;short_title&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="p">)),</span>
            <span class="p">(</span><span class="s">&#39;/&lt;pk&gt;/edit&#39;</span><span class="p">,</span> <span class="s">&#39;edit&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_view</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="s">&#39;short_title&#39;</span><span class="p">,</span>
                <span class="bp">self</span><span class="p">)),</span>
            <span class="p">(</span><span class="s">&#39;/&lt;pk&gt;/delete&#39;</span><span class="p">,</span> <span class="s">&#39;delete&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delete_view</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="s">&#39;short_title&#39;</span><span class="p">,</span>
                <span class="bp">self</span><span class="p">)),</span>
        <span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_object_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">search</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">order_by_field</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">order_by_direction</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns objects list ordered and filtered.</span>

<span class="sd">        :param search: The search string for quick filtering</span>
<span class="sd">        :param order_by_field: The ordering field</span>
<span class="sd">        :param order_by_direction: The ordering direction</span>
<span class="sd">        :param offset: The pagintation offset</span>
<span class="sd">        :param limit: The pagination limit</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">count_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">search</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Counts filtered object list.</span>

<span class="sd">        :param search: The search string for quick filtering.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_action_for_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns title and link for given list field and object.</span>

<span class="sd">        :param field: The field path.</span>
<span class="sd">        :param object: The line object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">title</span><span class="p">,</span> <span class="n">url</span> <span class="o">=</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span>
        <span class="n">field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_fields</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
        <span class="k">if</span> <span class="s">&#39;action&#39;</span> <span class="ow">in</span> <span class="n">field</span><span class="p">:</span>
            <span class="n">title</span> <span class="o">=</span> <span class="n">field</span><span class="p">[</span><span class="s">&#39;action&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;title&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">title</span><span class="p">):</span>
                <span class="n">title</span> <span class="o">=</span> <span class="n">title</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
            <span class="n">url</span> <span class="o">=</span> <span class="n">field</span><span class="p">[</span><span class="s">&#39;action&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;url&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">url</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">title</span><span class="p">,</span> <span class="n">url</span>

    <span class="k">def</span> <span class="nf">get_actions_for_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">object</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns action available for each object.</span>

<span class="sd">        :param object: The raw object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns form initialy populate from object instance.</span>

<span class="sd">        :param obj: The object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_class</span><span class="p">(</span><span class="n">obj</span><span class="o">=</span><span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns object retrieve by primary key.</span>

<span class="sd">        :param pk: The object primary key</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">create_object</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns new object instance.&quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">save_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">object</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Persits object.</span>

<span class="sd">        :param object: The object to persist</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">delete_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">object</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Deletes object.</span>

<span class="sd">        :param object: The object to delete</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <a href="http://github.com/jeanphix/Flask-Dashed"><img style="position: fixed; top: 0; right: 0; border: 0;"
    src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
  

  

  </body>
</html>